package com.payService.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.payService.Client.EduCourse;
import com.payService.Client.EduMember;
import com.payService.entity.TOrder;
import com.payService.vo.UcenterMember;
import com.payService.vo.courseMes;
import com.payService.service.ITOrderService;
import com.payService.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
public class TOrderController {

    @Autowired
    private ITOrderService itOrderService;

    @Autowired
    private EduMember eduMember;
    @Autowired
    private EduCourse eduCourse;

    /**
     * 创建订单
     *
     *
     * */
    @PostMapping("createOrder/{courseId}/{token}")
    public R createOrder(@PathVariable("courseId")String id, @PathVariable("token")String token){
        R mesByToken = eduMember.getMesByToken(token);
        Object member = mesByToken.getData().get("member");
        R courseByid = eduCourse.homeGetCourseInfo(id);
        Object courseInfo =courseByid.getData().get("courseInfoMes");
        String orderId = IdUtil.objectId();
        ObjectMapper objectMapper = new ObjectMapper();

        //Object 转 实体类
        courseMes courseMes = objectMapper.convertValue(courseInfo, courseMes.class);
        UcenterMember ucenterMember = objectMapper.convertValue(member, UcenterMember.class);
        System.out.println(courseMes);

        TOrder tOrder = new TOrder();
        tOrder.setOrderNo(orderId);
        tOrder.setCourseCover(courseMes.getCover());
        tOrder.setCourseId(courseMes.getId());
        tOrder.setCourseTitle(courseMes.getTitle());
        tOrder.setMobile(ucenterMember.getMobile());
        tOrder.setNickname(ucenterMember.getNickname());
        tOrder.setTeacherName(courseMes.getTeacherName());
        tOrder.setMemberId(ucenterMember.getId());
        tOrder.setPayType(1);
        tOrder.setTotalFee(courseMes.getPrice());
        tOrder.setStatus(0);

        itOrderService.save(tOrder);

        return R.ok().msg("创建订单成功！").data("orderId",orderId);

    }


    /**
     *
     * 根据订单号查询订单信息
     *
     * */
    @GetMapping("/getOrder/{orderId}")
    public R getOrderById(@PathVariable("orderId") String orderId){
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderId);
        TOrder order = itOrderService.getOne(queryWrapper);
        return R.ok().data("order",order);
    }

    /**
     *
     *查询当日的售课情况
     *
     * */
    @GetMapping("/getBuyCountToday/{date}")
    public R getBuyCountToday(@PathVariable("date") String date){
        Map<String,Integer> todayMap = itOrderService.getBuyCountToday(date);
        return R.ok().data("todayMap",todayMap);
    }
}
